package com.wyth.lottery.utils;

import java.util.Hashtable;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class QrCodeUtils {


	/**
	 * 生成二维码
	 * @param content：二维码内容
	 * @param filePath：保存路径
	 * 调用示例：QrCodeUtils.creatImg("56B6469E-E906D0CE(BEECC49F6D7CAE6B)","D:\\codeImg\\"+System.currentTimeMillis()+".png");
	 * @return 是否成功
	 */
    public static boolean creatImg(String content,String filePath){
    	 String format=filePath.substring(filePath.lastIndexOf(".")+1);
         int bian=200;  
         Hashtable<EncodeHintType, String> hints= new Hashtable<EncodeHintType, String>();
         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   
        BitMatrix bitMatrix;
		try {
			bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, bian, bian,hints);
			//1.1去白边
	          int[] rec = bitMatrix.getEnclosingRectangle();  
	          int resWidth = rec[2] + 1;  
	          int resHeight = rec[3] + 1;  
	          BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
	          resMatrix.clear();  
	          for (int i = 0; i < resWidth; i++) {  
	              for (int j = 0; j < resHeight; j++) {  
	                  if (bitMatrix.get(i + rec[0], j + rec[1])) { 
	                       resMatrix.set(i, j); 
	                  } 
	              }  
	          }  
	          //2
	          int width = resMatrix.getWidth();
	          int height = resMatrix.getHeight();
//	          BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
//	          for (int x = 0; x < width; x++) {
//	              for (int y = 0; y < height; y++) {
//	                  image.setRGB(x, y, resMatrix.get(x, y) == true ?
//	                  Color.BLACK.getRGB():Color.WHITE.getRGB());
//	              }
//	          }
//	          return ImageIO.write(image,format, new File(filePath));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
        return false;
    }

}
